package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.PageMessage;
import com.demo.core.exception.ParameterException;
import com.demo.dao.ScientificDirectionDao;
import com.demo.domain.ScientificDirection;
import com.demo.service.ScientificDirectionService;
import com.demo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ScientificDirectionServiceImpl extends ServiceImpl<ScientificDirectionDao, ScientificDirection> implements ScientificDirectionService {

    @Autowired
    ScientificDirectionDao scientificDirectionDao;

    @Autowired
    private StringUtils stringUtils;

    @Override
    public List<List<ScientificDirection>> getAllTeachersDirection() throws Exception {
        //按照组和科研序号排列好后的所有记录
        List<ScientificDirection> list0 = scientificDirectionDao.selectAllDirection();
        //list2每条记录为一个老师的所有方向
        List<List<ScientificDirection>> list2 = new ArrayList<>();
        //目前所含有的科研方向的老师数量
        int num = scientificDirectionDao.selectTeaNum();
        list2 = stringUtils.listByTeacher(list0, num);
        return list2;
    }

    /**
     * 得到教师的科研方向（所有），有分页
     *
     * @param current
     * @param size
     * @return Page类型
     */
    @Override
    public Page getTeaDirectionByPage(int current, int size) throws ParameterException {
        if (current == 0 || size == 0) {
            throw new ParameterException("current和size含0");
        }
        //按照组和科研序号排列好后的所有记录
        List<ScientificDirection> list0 = scientificDirectionDao.selectAllDirection();
        //list2每条记录为一个老师的所有方向
        List<List<ScientificDirection>> list2 = new ArrayList<>();
        //目前所含有的科研方向的老师数量
        int num = scientificDirectionDao.selectTeaNum();
        list2 = stringUtils.listByTeacher(list0, num);
        //对list2进行分页
        PageMessage<List<ScientificDirection>> pm = new PageMessage<>(current, size);
        try {
            if (list2.size() > 0 && list2 != null) {
                return pm.pageForList(list2);
            }
        } catch (ParameterException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 返回几个教师的科研方向
     *
     * @param number
     * @return List<List < ScientificDirection>>
     */
    @Override
    public List<List<ScientificDirection>> getFewTeacherDirection(int number) throws ParameterException {
        if (number <= 0) {
            throw new ParameterException("未指定需求教师个数");
        }
        List<String> teacherName = scientificDirectionDao.selectLateTeacherName(number);
        return getSeveralTeaDirection(teacherName);
    }


    /**
     * 输入随意个教师名得到教师的科研方向
     *
     * @param namelist
     * @return
     */
    @Override
    public List<List<ScientificDirection>> getSeveralTeaDirection(List<String> namelist) throws ParameterException {
        if (namelist == null || namelist.size() == 0) {
            throw new ParameterException("传入的nameList为空");
        }
        LambdaQueryWrapper<ScientificDirection> lqw = new LambdaQueryWrapper<ScientificDirection>();
//        根据列表长度动态地将查询的教师用or连接到LambdaQueryWrapper后面

        lqw.and(scientificDirectionLambdaQueryWrapper -> {
            for (String name : namelist) {
                scientificDirectionLambdaQueryWrapper.or(wrapper -> wrapper.like(ScientificDirection::getSdName, name.trim()));
            }
        });
//        查询
        lqw.eq(ScientificDirection::getSdIsDelete,0);
        List<ScientificDirection> list0 = scientificDirectionDao.selectList(lqw);
        List<List<ScientificDirection>> list2 = new ArrayList<>();
        //目前所含有的科研方向的老师数量
        int num = scientificDirectionDao.selectTeaNum();
//        将查询到的记录按教师名称分类
        list2 = stringUtils.listByTeacher(list0, num);
//        检测是否为空
        if (list2 == null) {
            return list2;
        } else {
//                    list3用来存储排序好后的结果，同时为返回数据
            List<List<ScientificDirection>> list3 = new ArrayList<>();
//                    得到所有有科研方向的教师的名称
            List<String> teaName = scientificDirectionDao.selectTeaName();
//                    将namelist中有搜索结果的教师的名字筛选出来放到containList中
            List<String> containList = teaName.stream()
                    .filter(tea -> namelist.stream().anyMatch(name -> tea.contains(name.trim())))
                    .collect(Collectors.toList());
//            按列表的教师顺序排序list2
            for (String na : containList) {
                Optional<List<ScientificDirection>> optional1 = list2.stream()
                        .filter(a -> a.get(0).getSdName().contains(na))
                        .findFirst();
                list3.add(optional1.get());
            }
            return list3;
        }
    }

    @Override
    public LambdaQueryWrapper<ScientificDirection> searchScientificDirection(String sdName, String sdDirectionName) throws ParameterException {
        sdName = sdName == null ? "" : sdName.trim();
        sdDirectionName = sdDirectionName == null ? "" : sdDirectionName.trim();
        LambdaQueryWrapper<ScientificDirection> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!sdName.equals(""), ScientificDirection::getSdName, sdName);
        wrapper.like(!sdDirectionName.equals(""), ScientificDirection::getSdDirectionName, sdDirectionName);
        wrapper.eq(ScientificDirection::getSdIsDelete,0);
        return wrapper;
    }


    @Override
    public List<ScientificDirection> deleteScientificDirection(List<Long> sdIds) throws ParameterException {
        if (sdIds == null || sdIds.size() < 1) {
            throw new ParameterException("无删除事项");
        }
        List<ScientificDirection> sdList = new ArrayList<>();
        for (Long sdId : sdIds) {
            if (sdId != null && sdId > 0l) {
                ScientificDirection sd = new ScientificDirection();
                sd.setSdId(sdId);
                sd.setSdIsDelete(1);//1为已删除
                sdList.add(sd);
            }
        }
        return sdList;
    }


}
